package com.songlanyun.modules.share.service.impl;

import com.songlanyun.common.enums.OpusConstant;
import com.songlanyun.common.utils.RedisKeys;
import com.songlanyun.modules.goods.entity.Goods;
import com.songlanyun.modules.goods.service.GoodsService;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyActivityGoods;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyActivityGoodsService;
import com.songlanyun.modules.marketing.seckill.entity.SeckillActivityGoods;
import com.songlanyun.modules.marketing.seckill.service.SeckillActivityGoodsService;
import com.songlanyun.modules.servicegoods.entity.ServiceGoods;
import com.songlanyun.modules.servicegoods.service.ServiceGoodsService;
import com.songlanyun.modules.share.service.GoodsShareService;
import com.songlanyun.modules.share.service.ShareRecordService;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2021-01-13 16:35:02
 */
@Service("goodsShareService")
public class GoodsShareServiceImpl implements GoodsShareService {

    @Resource
    private ShareRecordService shareRecordService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private ServiceGoodsService serviceGoodsService;
    @Resource
    private SeckillActivityGoodsService seckillActivityGoodsService;
    @Resource
    private GroupBuyActivityGoodsService groupBuyActivityGoodsService;


    @Override
    @Transactional
    public void shareSave(RedisKeys.BbsKey redisKey, BoundHashOperations<String, String, Object> result) {
        //作品分享
        result.entries().forEach((m, n) -> {
            HashMap<String, Object> userMap = (HashMap<String, Object>) n;//{11=111, 22=222}

            //获得商品ID
            Long goodsId = Long.parseLong(m);
            Long publisherId = null;//发布商品的商家
            switch (redisKey) {
                case SHARE_COMMON_GOODS_KEY:
                    Goods goods = goodsService.getById(goodsId);
                    if (goods != null) {
                        publisherId = goods.getShopId();
                    }
                    break;
                case SHARE_SERVICE_GOODS_KEY:
                    ServiceGoods serviceGoods = serviceGoodsService.getById(goodsId);
                    if (serviceGoods != null) {
                        publisherId = serviceGoods.getShopId();
                    }
                    break;
                case SHARE_SECKILL_GOODS_KEY:
                    SeckillActivityGoods seckillActivityGoods = seckillActivityGoodsService.getById(goodsId);
                    if (seckillActivityGoods != null) {
                        publisherId = seckillActivityGoods.getShopId();
                    }
                    break;
                case SHARE_GROUP_BUY_GOODS_KEY:
                    GroupBuyActivityGoods groupBuyActivityGoods = groupBuyActivityGoodsService.getById(goodsId);
                    if (groupBuyActivityGoods != null) {
                        publisherId = groupBuyActivityGoods.getShopId();
                    }
                    break;
            }

            if (publisherId != null && userMap != null) {
                Long finalPublisherId = publisherId;
                userMap.forEach((key, value) -> {
                    Long userId = Long.parseLong(key);//11
                    Map<String, Object> numTimeMap = (Map<String, Object>) value;//111
                    Date time = (Date) numTimeMap.get("time");//最后一次更新时间

                    //保存分享记录
                    shareRecordService.share(OpusConstant.ModelType.COMMON_GOODS, goodsId, userId, finalPublisherId, time);

                });
            }
            //删除redis缓存
            result.delete(m);
        });
    }


}

